package com.eveinfo.display;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import com.eveinfo.EveInfo;
import com.eveinfo.R;
import com.eveinfo.api.character.sheet.ApiSheet;
import com.eveinfo.display.skills.AllSkillsFilter;
import com.eveinfo.display.skills.KnowSkillsFilter;
import com.eveinfo.display.skills.SkillsAdapter;
import com.eveinfo.display.skills.SkillsFilter;
import com.eveinfo.display.skills.TrainableSkillsFilter;

public class CharSkillView extends Activity {

	private static ApiSheet sheet;

	private enum Items {
		all("All skills", AllSkillsFilter.getInstance()),
		known("Known skills", new KnowSkillsFilter(sheet.getSkills())),
		trainable("Trainable", new TrainableSkillsFilter(sheet.getSkills()));

		private final String name;
		private final SkillsFilter filter;

		private Items(String name, SkillsFilter filter) {
			this.name = name;
			this.filter = filter;
		}

		public SkillsFilter getFilter() {
			return filter;
		}

		@Override
		public String toString() {
			return name;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.skills);

		int characterID = getIntent().getIntExtra("characterID", 0);
		sheet = EveInfo.getData().getCharacterSheet(characterID);
		if (sheet == null) {
			// TODO
			finish();
			return;
		}

		ListView listview = (ListView) findViewById(R.id.LVSkills);
		final SkillsAdapter skillsAdapter = new SkillsAdapter(this, listview, EveInfo.getData().getSkills()
				.getSkillGroups(), sheet);

		final Spinner spinner = (Spinner) findViewById(R.id.SSkills);
		ArrayAdapter<Items> adapter = new ArrayAdapter<Items>(this, android.R.layout.simple_spinner_item, Items
				.values());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Items i = (Items) spinner.getSelectedItem();
				skillsAdapter.setFilter(i.getFilter());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}

}
